namespace UnitTests
{
    using System;
    using System.Collections.Specialized;
    using System.Linq;
    using System.Xml.Linq;
    using FormStateKeeper;
    using NUnit.Framework;
    using UnitTests.Foundation;

    [TestFixture]
    public class FormStateRestoreHandlerTests : BaseTestFixture
    {
        [Test]
        public void IsReusable_False()
        {
            var sut = new FormStateRestoreHandler(new FormState("1", String.Empty, new NameValueCollection()));

            Assert.False(sut.IsReusable);
        }

        [Test]
        public void ProcessRequest_RendersOutput()
        {
            using (var context = new TestContextBuilder<WebTestContext>().Create())
            {
                var sut = new FormStateRestoreHandler(new FormState("1", String.Empty, new NameValueCollection()));
                sut.ProcessRequest(context.HttpContextMock.Object);

                Assert.Greater(context.ResponseMock.Object.Output.ToString().Length, 0);
            }
        }

        [Test]
        public void ProcessRequest_RendersWellFormedOutput()
        {
            using (var context = new TestContextBuilder<WebTestContext>().Create())
            {
                var sut = new FormStateRestoreHandler(new FormState("1", String.Empty, new NameValueCollection()));
                sut.ProcessRequest(context.HttpContextMock.Object);

                XDocument dom = XDocument.Parse(context.ResponseMock.Object.Output.ToString());
                Assert.NotNull(dom.Root);
            }
        }

        [Test]
        public void ProcessRequest_RendersHiddenFormFieldFromFormState()
        {
            using (var context = new TestContextBuilder<WebTestContext>().Create())
            {
                var sut = new FormStateRestoreHandler(new FormState("1", String.Empty, new NameValueCollection { { "field1", "value1" } }));
                sut.ProcessRequest(context.HttpContextMock.Object);

                XDocument dom = XDocument.Parse(context.ResponseMock.Object.Output.ToString());
                Assert.True(dom.Root.Descendants("input").Any(elem =>
                                                          elem.Attribute("name").Value == "field1" &&
                                                          elem.Attribute("value").Value == "value1" &&
                                                          elem.Attribute("type").Value == "hidden"));
            }
        }

        [Test]
        public void ProcessRequest_UpdatePanelAsyncPost_AlwaysSetsAsyncPostFieldToFalse()
        {
            using (var context = new TestContextBuilder<WebTestContext>().Create())
            {
                var sut = new FormStateRestoreHandler(new FormState("1", String.Empty, new NameValueCollection { { "__ASYNCPOST", "true" } }));
                sut.ProcessRequest(context.HttpContextMock.Object);

                XDocument dom = XDocument.Parse(context.ResponseMock.Object.Output.ToString());
                Assert.True(dom.Root.Descendants("input").Any(elem =>
                                                          elem.Attribute("name").Value == "__ASYNCPOST" &&
                                                          elem.Attribute("value").Value == "false" &&
                                                          elem.Attribute("type").Value == "hidden"));
            }
        }
    }
}